package com.itheima.d7_thread_communication;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private int foodNum;
    private List<String> foods = new ArrayList<>();

    public synchronized void create() {

        try {
            final String name = Thread.currentThread().getName();
            if (foods.size() == 0) {
                Thread.sleep(1000);
                foodNum++;
                System.out.println(name + "做了第" + foodNum + "个包子.");
                foods.add("baozi");
            }
            this.notify();
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public synchronized void eat() {
        final String name = Thread.currentThread().getName();

        try {
            if (foods.size() > 0) {
                Thread.sleep(700);
                System.out.println(name + "吃了第" + foodNum + "个包子.");
                foods.remove(0);
            }
            this.notify();
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
